<?php

namespace app\controller\System;

use app\model\SystemConfig;
use think\Request;
use think\Response;

class SystemConfigController extends BaseController
{
    /**
     * 显示配置列表
     */
    public function index()
    {
        $group = request()->param('group', 'external_api');
        $configs = SystemConfig::getConfigsByGroup($group);
        
        return $this->success([
            'configs' => $configs,
            'group' => $group
        ], '获取配置成功');
    }

    /**
     * 获取单个配置
     */
    public function show($id)
    {
        $config = SystemConfig::find($id);
        if (!$config) {
            return $this->error('配置不存在', 404);
        }

        return $this->success($config, '获取配置成功');
    }

    /**
     * 更新配置
     */
    public function update(Request $request)
    {
        $data = $request->post();
        
        // 验证数据
        $validate = [
            'configs' => 'require|array'
        ];
        $message = [
            'configs.require' => '配置数据不能为空',
            'configs.array' => '配置数据格式错误'
        ];
        
        try {
            validate($validate, $message)->check($data);
        } catch (\Exception $e) {
            return $this->error($e->getMessage(), 400);
        }

        // 批量更新配置
        $configs = $data['configs'];
        $success = SystemConfig::batchUpdate($configs);
        
        if ($success) {
            return $this->success(null, '配置更新成功');
        } else {
            return $this->error('配置更新失败', 500);
        }
    }

    /**
     * 获取外部API配置
     */
    public function getExternalApiConfig()
    {
        $configs = SystemConfig::getConfigsByGroup('external_api');
        
        $result = [];
        foreach ($configs as $config) {
            $result[$config['config_key']] = [
                'name' => $config['config_name'],
                'value' => $config['config_value'],
                'description' => $config['description'],
                'is_required' => $config['is_required'],
                'is_encrypted' => $config['is_encrypted']
            ];
        }
        
        return $this->success($result, '获取外部API配置成功');
    }

    /**
     * 更新外部API配置
     */
    public function updateExternalApiConfig(Request $request)
    {
        $data = $request->post();
        
        // 验证必填字段
        $requiredFields = [
            'xiaofeng_order_token' => '晓风订单Token',
            'xiaofeng_create_order_token' => '晓风下单Token'
        ];
        
        foreach ($requiredFields as $field => $name) {
            if (empty($data[$field])) {
                return $this->error($name . '不能为空', 400);
            }
        }
        
        // 更新配置
        $success = SystemConfig::batchUpdate($data);
        
        if ($success) {
            return $this->success(null, '外部API配置更新成功');
        } else {
            return $this->error('外部API配置更新失败', 500);
        }
    }

    /**
     * 测试Token连接
     */
    public function testToken()
    {
        $orderToken = SystemConfig::getConfig('xiaofeng_order_token');
        $createOrderToken = SystemConfig::getConfig('xiaofeng_create_order_token');
        
        $results = [];
        
        // 测试晓风订单Token
        if (!empty($orderToken)) {
            $results['xiaofeng_order_token'] = [
                'status' => 'success',
                'message' => 'Token已配置'
            ];
        } else {
            $results['xiaofeng_order_token'] = [
                'status' => 'error',
                'message' => 'Token未配置'
            ];
        }
        
        // 测试晓风下单Token
        if (!empty($createOrderToken)) {
            $results['xiaofeng_create_order_token'] = [
                'status' => 'success',
                'message' => 'Token已配置'
            ];
        } else {
            $results['xiaofeng_create_order_token'] = [
                'status' => 'error',
                'message' => 'Token未配置'
            ];
        }
        
        return $this->success($results, 'Token测试完成');
    }
} 